package inet.app.vod.facebook;

import inet.app.vod.activity.R;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.content.Context;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.inet.app.framework.util.Log;

public class CommentView extends RelativeLayout {

	public final static String BASE_URL = "http://iphim.vn";

	private WebView webView;
	private WebView childView;
	private Context context;

	private boolean isStarted = false;

	public CommentView(Context context) {
		super(context);
		init(context);
	}

	public CommentView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public CommentView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	private void init(Context context) {
		this.context = context;

		// setting webview
		webView = new WebView(context);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.setWebViewClient(new FaceBookClient());
		webView.setWebChromeClient(new MyChromeClient());
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setAppCacheEnabled(true);
		webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
		webView.getSettings().setSupportMultipleWindows(true);
		RelativeLayout.LayoutParams fullParams = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.MATCH_PARENT,
				RelativeLayout.LayoutParams.MATCH_PARENT);
		webView.setLayoutParams(fullParams);
	}

	public void loadComments(String url) {

		this.isStarted = true;
		this.addView(webView);

		// load file html to webview
		StringBuilder buf = new StringBuilder();
		InputStream json;
		try {
			json = context.getAssets().open("comment.html");
			BufferedReader in = new BufferedReader(new InputStreamReader(json));
			String str;
			while ((str = in.readLine()) != null) {
				buf.append(str);
			}
			in.close();
			String contentHtml = buf.toString();
			contentHtml = contentHtml.replaceFirst("data-href=\".*\"",
					"data-href=\"" + url + "\"");
			Log.d("start load comment facebook");
			webView.loadDataWithBaseURL(BASE_URL, contentHtml, "text/html",
					null, null);

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private class MyChromeClient extends WebChromeClient {
		@Override
		public boolean onCreateWindow(WebView view, boolean dialog,
				boolean userGesture, Message resultMsg) {
			childView = new WebView(context);
			childView.getSettings().setJavaScriptEnabled(true);
			childView.setWebViewClient(new FaceBookClient());
			childView.setWebChromeClient(this);
			childView.setLayoutParams(new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.MATCH_PARENT,
					LinearLayout.LayoutParams.WRAP_CONTENT));
			CommentView.this.addView(childView);

			childView.requestFocus();
			webView.setVisibility(View.GONE);

			WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
			transport.setWebView(childView);
			resultMsg.sendToTarget();
			return true;
		}

		@Override
		public void onProgressChanged(WebView view, int newProgress) {
		}

		@Override
		public void onCloseWindow(WebView window) {
			RelativeLayout parentLayout = (RelativeLayout) findViewById(R.id.comment_layout);
			parentLayout.removeViewAt(parentLayout.getChildCount() - 1);
			childView = null;
			webView.setVisibility(View.VISIBLE);
			webView.requestFocus();
		}
	}

	private class FaceBookClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			return false;
		}

	}

	public boolean isStarted() {
		return isStarted;
	}

}
